#!/usr/bin/env bash

cp $1.in $1

uname=`uname`

if [[ "$uname" == 'SunOS' ]]; then
	echo Configuring $1 for OpenSolaris
	sed -i 's/-DflagPOSIX -DflagLINUX/-DflagPOSIX -DflagSOLARIS/' $1
	sed -i 's/-Wl,--gc-sections $(LINKOPTIONS)/$(LINKOPTIONS)/' $1
	sed -i 's/GCC-Gcc-Gui-Linux-Main-Posix-Shared/GCC-Gcc-Gui-Main-Posix-Shared-Solaris/' $1
	sed -i 's/GCC-Gcc-Gui-Linux-Posix-Shared/GCC-Gcc-Gui-Posix-Shared-Solaris/' $1
	sed -i 's/GCC-Gcc-Linux-Main-Posix-Shared/GCC-Gcc-Main-Posix-Shared-Solaris/' $1
	sed -i 's/GCC-Gcc-Linux-Posix-Shared/GCC-Gcc-Posix-Shared-Solaris/' $1
	sed -i 's/$(LIBPATH) -Wl,-O,2 $(LDFLAGS)/$(LIBPATH) $(LDFLAGS)/' $1
	sed -i 's/-lpthread /-lposix4 /' $1
	sed -i 's/-lrt /-lsocket -lnsl /' $1
fi

if [[ "$uname" == 'FreeBSD' ]]; then
	echo Configuring $1 for FreeBSD
	sed -i '' 's/-DflagPOSIX -DflagLINUX/-DflagPOSIX -DflagBSD -DflagFREEBSD/' $1
	sed -i '' 's/-Wl,--gc-sections $(LINKOPTIONS)/$(LINKOPTIONS)/' $1
	sed -i '' 's/GCC-Gcc-Gui-Linux-Main-Posix-Shared/GCC-Bsd-Freebsd-Gcc-Gui-Main-Posix-Shared/' $1
	sed -i '' 's/GCC-Gcc-Gui-Linux-Posix-Shared/GCC-Bsd-Freebsd-Gcc-Gui-Posix-Shared/' $1
	sed -i '' 's/GCC-Gcc-Linux-Main-Posix-Shared/GCC-Bsd-Freebsd-Gcc-Main-Posix-Shared/' $1
	sed -i '' 's/GCC-Gcc-Linux-Posix-Shared/GCC-Bsd-Freebsd-Gcc-Posix-Shared/' $1
	sed -i '' 's/$(LIBPATH) -Wl,-O,2 $(LDFLAGS)/$(LIBPATH) $(LDFLAGS)/' $1
	sed -i '' 's/-ldl /-lexecinfo /' $1
fi

if [[ "$uname" == 'OpenBSD' ]]; then
	echo Configuring $1 for OpenBSD
	sed -i.bak 's/-DflagPOSIX -DflagLINUX/-DflagPOSIX -DflagBSD -DflagOPENBSD/' $1
	sed -i.bak 's/-Wl,--gc-sections $(LINKOPTIONS)/$(LINKOPTIONS)/' $1
	sed -i.bak 's#LIBPATH =#LIBPATH = -L"/usr/local/lib"#' $1
	sed -i.bak 's#-I./ -I$(UPPOUT)#-I/usr/local/include -I./ -I$(UPPOUT)#' $1
	sed -i.bak 's/GCC-Gcc-Gui-Linux-Main-Posix-Shared/GCC-Bsd-Gcc-Gui-Main-Openbsd-Posix-Shared/' $1
	sed -i.bak 's/GCC-Gcc-Gui-Linux-Posix-Shared/GCC-Bsd-Gcc-Gui-Openbsd-Posix-Shared/' $1
	sed -i.bak 's/GCC-Gcc-Linux-Main-Posix-Shared/GCC-Bsd-Gcc-Main-Openbsd-Posix-Shared/' $1
	sed -i.bak 's/GCC-Gcc-Linux-Posix-Shared/GCC-Bsd-Gcc-Openbsd-Posix-Shared/' $1
	sed -i.bak 's/$(LIBPATH) -Wl,-O,2 $(LDFLAGS)/$(LIBPATH) $(LDFLAGS)/' $1
	sed -i.bak 's/-ldl /-lexecinfo /' $1
	sed -i.bak 's/-lrt / /' $1
	rm  $1.bak
fi

if [[ "$uname" == 'NetBSD' ]]; then
	echo Configuring $1 for OpenBSD
	sed -i.bak 's/-DflagPOSIX -DflagLINUX/-DflagPOSIX -DflagBSD -DflagNETBSD/' $1
	sed -i.bak 's/-Wl,--gc-sections $(LINKOPTIONS)/$(LINKOPTIONS)/' $1
	sed -i.bak 's#LIBPATH =#LIBPATH = -L"/usr/local/lib"#' $1
	sed -i.bak 's#-I./ -I$(UPPOUT)#-I/usr/local/include -I./ -I$(UPPOUT)#' $1
	sed -i.bak 's/$(LIBPATH) -Wl,-O,2 $(LDFLAGS)/$(LIBPATH) $(LDFLAGS)/' $1
	sed -i.bak 's/-ldl /-lexecinfo /' $1
	sed -i.bak 's/-lrt / /' $1
	rm  $1.bak
fi

if [[ "$uname" == 'Darwin' ]]; then
	echo Configuring $1 for MacOS
	sed -i.bak 's/-DflagPOSIX -DflagLINUX/-DflagPOSIX -DflagBSD -DflagOSX/' $1
	sed -i.bak 's/-Wl,--gc-sections $(LINKOPTIONS)/$(LINKOPTIONS)/' $1
	sed -i.bak 's/\`pkg-config --cflags .*\`/ /' $1
	sed -i.bak 's/\`pkg-config --libs .*\`/ /' $1
	sed -i.bak 's/-lrt / -framework Foundation -framework Cocoa -framework Carbon /' $1
	sed -i.bak 's/-lpng / /' $1
	sed -i.bak 's/-Wl,-s/ /' $1
	sed -i.bak 's/-Wl,--start-group/ /' $1
	sed -i.bak 's/-Wl,--end-group/ /' $1
	sed -i.bak 's/$(LIBPATH) -Wl,-O,2 $(LDFLAGS)/$(LIBPATH) $(LDFLAGS)/' $1
fi
